package com.example.tester.dao.impl;

import com.example.tester.dao.StudentDao;
import com.example.tester.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.Types;
import java.util.List;
import java.util.Map;

/**
 * @author lzh
 * @date 2019/9/6 - 16:46
 */
@Repository
public class StudentDaoImpl implements StudentDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public int add(Student student) {
         String sql = "insert into student(sno,sname,ssex) values(?,?,?)";
         Object[] args = { student.getSno(), student.getSname(), student.getSsex() };
         int[] argTypes = { Types.INTEGER, Types.VARCHAR, Types.VARCHAR };
         return this.jdbcTemplate.update(sql, args, argTypes);
    }

    @Override
    public int add2(Student student) {
        String sql = "insert into student(sno,sname,ssex) values(:sno,:sname,:ssex)";
        NamedParameterJdbcTemplate npjt = new NamedParameterJdbcTemplate(this.jdbcTemplate.getDataSource());
        return npjt.update(sql, new BeanPropertySqlParameterSource(student));
    }

    @Override
    public int update(Student student) {
        return 0;
    }

    @Override
    public int deleteBySno(int sno) {
        String sql = "delete from student where sno = ?";
        Object[] args = { sno };
        int[] argTypes = { Types.INTEGER };
        return this.jdbcTemplate.update(sql, args, argTypes);
    }

    @Override
    public List<Map<String, Object>> queryStudentsListMap() {
        return null;
    }

    @Override
    public Student queryStudentBySno(String sno) {
        return null;
    }
}
